package com.xixilala.video.render

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.opengl.GLUtils
import com.xixilala.video.R
import com.xixilala.video.util.ShaderUtil
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10


/***
 * https://blog.csdn.net/china0851/article/details/85156167
 */
class ImageRender(private val context:Context):GLSurfaceView.Renderer {





    override fun onSurfaceCreated(gl: GL10, config: EGLConfig?) {
        gl.glClearColor(0f,0f,0f,0f)

    }

    override fun onSurfaceChanged(gl: GL10, width: Int, height: Int) {
        gl.glViewport(0,0,width,height)


    }

    override fun onDrawFrame(gl: GL10) {

        gl.glClearColor(0f,0f,0f,0f)


    }



    /***
     * 1.vertex
     * 2.fragment
     * 3.vPosition
     * 4.vColor
     * 5.
     */

    class Square(val context: Context) {

        var programId = -1
        var positionHandler= -1
        var colorHandler= -1

        val vertexCoords = floatArrayOf(
            -0.5f, 0.5f, 0.0f, // top left
            -0.5f, -0.5f, 0.0f, // bottom left
            0.5f, -0.5f, 0.0f, // bottom right
            0.5f, 0.5f, 0.0f  // top right
        )
        val vertexIndex = shortArrayOf(
            0, 1, 2, 0, 3, 2
        )

        val COOR_PER_VERTEX = 3
        val vertexStride = COOR_PER_VERTEX*4

        var vertexBuffer:FloatBuffer = ByteBuffer.allocateDirect(vertexCoords.size*4).run {
            order(ByteOrder.nativeOrder())

            asFloatBuffer().apply {
                put(vertexCoords)
                position(0)
            }
        }

        private val VERTEX_SOURCE =
                "attribute vec4 vPosition;\n" +
                        "attribute vec2 vCoordinate;\n" +
                        "varying vec2 aCoordinate;\n" +
                        "void main(){\n" +
                        "    gl_Position=vPosition;\n" +
                        "    aCoordinate=vCoordinate;\n" +
                        "}"
        private val FRAGMENT_SOURCE =
                "precision mediump float;\n" +
                        "uniform sampler2D vTexture;\n" +
                        "varying vec2 aCoordinate;\n" +
                        "void main(){\n" +
                        "    gl_FragColor=texture2D(vTexture,aCoordinate);\n" +
                        "}"

        init {

            //处理顶点
            val allocate = ByteBuffer.allocate(vertexIndex.size * 2)
            allocate.order(ByteOrder.nativeOrder())
            val asShortBuffer = allocate.asShortBuffer().also {
                it.put(vertexIndex)
                it.position(0)
            }


            programId = GLES20.glCreateProgram()

            val vertexShader = ShaderUtil.loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SOURCE)
            val fragmentShader = ShaderUtil.loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SOURCE)

            GLES20.glAttachShader(programId,vertexShader)
            GLES20.glAttachShader(programId,fragmentShader)

            GLES20.glLinkProgram(programId)



        }

        fun drawImage(): Bitmap? {
            return BitmapFactory.decodeResource(context.resources, R.drawable.image)
        }

        private fun createTexture(): Int {
            val texure = IntArray(1)

            val drawImage = drawImage()
            if (drawImage != null && !drawImage.isRecycled) {

                //生成纹理
                GLES20.glGenTextures(1,texure,0)
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,texure[0])
                GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST.toFloat())
                GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR.toFloat())
                GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE.toFloat())
                GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE.toFloat())
                GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0,drawImage,0)
                return texure[0]
            }
            return 0
        }

        fun draw() {

            GLES20.glUseProgram(programId)

            positionHandler = GLES20.glGetAttribLocation(programId, "vPosition").also {
                GLES20.glEnableVertexAttribArray(it)
                GLES20.glVertexAttribPointer(
                    it,
                    COOR_PER_VERTEX,
                    GLES20.GL_FLOAT,
                    false,
                    vertexStride,
                    vertexBuffer
                )
            }

//            GLES20.glUniform1i()






        }



    }
}